#!/bin/bash
###
 # @Author: dvlproad
 # @Date: 2023-03-03 11:04:12
 # @LastEditors: dvlproad
 # @LastEditTime: 2023-06-09 12:56:39
 # @Description: 根据 ENVIRONMENT_FILE_PATH 中的环境，从 Notification_Argument_FILE_PATH 中获取该环境的值，设置到 ENVIRONMENT_FILE_PATH 中 package_notification_argument_current 键值
### 
#sh notificationParamsJsonFile_deal.sh -notificationArgJsonF "${Notification_Argument_FILE_PATH}" -envJsonF "${ENVIRONMENT_FILE_PATH}"
#sh notificationParamsJsonFile_deal.sh -notificationArgJsonF "../example_packing_info/notification_robot.json" -envJsonF "../example_packing_info/app_info.json"

#echo "===========进入脚本$0==========="

JQ_EXEC=`which jq`

# shell 参数具名化
show_usage="args: [-notificationArgJsonF, -envJsonF]\
                                  [--notification-argument-json-file=, --environment-json-file=]"

while [ -n "$1" ]
do
        case "$1" in
                -notificationArgJsonF|--notification-argument-json-file) Notification_Argument_FILE_PATH=$2; shift 2;;
                -notificationArgPeople|--notification-argument-people) Notification_Argument_PEOPLE=$2; shift 2;;
                -notificationArgMsgType|--notification-argument-msgtype) Notification_Argument_MSGTYPE=$2; shift 2;;
                -envJsonF|-environment-json-file) ENVIRONMENT_FILE_PATH=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}/..
bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
#echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"

qscript_path_get_filepath="${bulidScriptCommon_dir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"

source ${bulidScriptCommon_dir_Absolute}/a_function.sh ${bulidScriptCommon_dir_Absolute} # 为了使用 updatePackageErrorCodeAndMessage


if [ -n "${Notification_Argument_PEOPLE}" ];then
    sh "${qbase_update_json_file_singleString_script_path}" -jsonF "${ENVIRONMENT_FILE_PATH}" -k 'package_noti_people' -v "${Notification_Argument_PEOPLE}"
    if [ $? != 0 ]; then
        PackageErrorCode=-1
        PackageErrorMessage="❌Error:更新 package_noti_people 失败"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script
    fi
fi


if [ -n "${Notification_Argument_MSGTYPE}" ];then
    sh "${qbase_update_json_file_singleString_script_path}" -jsonF "${ENVIRONMENT_FILE_PATH}" -k 'package_noti_msgtype' -v "${Notification_Argument_MSGTYPE}"
    if [ $? != 0 ]; then
        PackageErrorCode=-1
        PackageErrorMessage="❌Error:更新 package_noti_msgtype 失败"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script
    fi
fi




echo "Notification_Argument_FILE_PATH=${Notification_Argument_FILE_PATH}"
if [ ! -f "${Notification_Argument_FILE_PATH}" ];then
    PackageErrorCode=-1
    PackageErrorMessage="你的Notification_Argument_FILE_PATH=${Notification_Argument_FILE_PATH}，未填写后者不存在，请检查"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi

check_jsonFile_valid ${Notification_Argument_FILE_PATH}
if [ $? != 0 ]; then
    exit_script
fi
  

source ${bulidScriptCommon_dir_Absolute}/a_source_env.sh -appInfoF "${ENVIRONMENT_FILE_PATH}" # 获取 BRANCH\PlatformType\PackageTargetType\PackageNetworkType
echo "------PackageNetworkType:${PackageNetworkType}"



# 是数组时候
network_notificationRootMapString=$(cat ${Notification_Argument_FILE_PATH} | ${JQ_EXEC} -r ".[]" | ${JQ_EXEC} -r "select(.network==\"${PackageNetworkType}\")")
#echo "===select result: network_notificationRootMapString=${network_notificationRootMapString}"
if [ "${network_notificationRootMapString}" == "null" ] || [ -z "${network_notificationRootMapString}" ]; then
    PackageErrorCode=-1
    PackageErrorMessage="您的${Notification_Argument_FILE_PATH}文件中未设置${PackageNetworkType}环境的notification通知参数，请检查"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    
    exit_script # 这是严重错误，所以直接退出本脚本，没必要继续执行
fi


function updateCurrentNotificationJsonData() {
    sh ${bulidScriptCommon_dir_Absolute}/update_json_file.sh -f "${ENVIRONMENT_FILE_PATH}" -k "package_notification_argument_current" -v "${network_notificationRootMapString}"

    # 验证上面的赋值结果是否成功
    notificationParamsRootMapString=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r ".package_notification_argument_current")
    echo "===get result: notificationParamsRootMapString=${notificationParamsRootMapString}"
    if [ "${notificationParamsRootMapString}" == "null" ] || [ -z "${notificationParamsRootMapString}" ]; then
        PackageErrorCode=-1
        PackageErrorMessage="你的${ENVIRONMENT_FILE_PATH}中未添加package_notification_argument_current参数，会导致无法发送打包过程中的信息"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script # 这是严重错误，所以直接退出本脚本，没必要继续执行
    fi
}
updateCurrentNotificationJsonData


# 测试能否取到值
ContentErrorTypeKey='other'
Wechat_ROBOT_URL=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.package_notification_argument_current.wechat') # -r 去除字符串引号
Wechat_CurrentErrorType_ROBOT_URL=$(echo ${Wechat_ROBOT_URL} | ${JQ_EXEC} -r --arg ContentErrorTypeKey "$ContentErrorTypeKey" '.[$ContentErrorTypeKey]' | ${JQ_EXEC} -r '.value') # -r 去除字符串引号
echo "${ContentErrorTypeKey}错误类型的Wechat_CurrentErrorType_ROBOT_URL=${Wechat_CurrentErrorType_ROBOT_URL}"
